package hdelsof

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
	"wu-yan-zhu/cache"
	"wu-yan-zhu/melsif"
	"wu-yan-zhu/pkg"
)

func Dl(c *gin.Context) {
	name := c.PostForm("name")
	passes := c.PostForm("passes")
	if name == "" || passes == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	redis_key := "redis_dl" + name
	po, _ := cache.Rdb.Get(cache.Ctx, redis_key).Int()
	if po >= 3 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误3次,禁止登录",
		})
		return
	}
	var user melsif.User
	err := melsif.DB.Where("name=?", name).First(&user).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "用户查询失败",
		})
		return
	}
	if user.ID == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "用户不存在",
		})
		return
	}
	if user.Passes != pkg.MD5(passes) {
		cache.Rdb.Incr(cache.Ctx, redis_key)
		if po == 2 {
			cache.Rdb.Set(cache.Ctx, redis_key, 3, time.Hour*2)
			c.JSON(200, gin.H{
				"code": 500,
				"msg":  "密码错误3次,禁止2个小时内登录",
			})
			return
		}
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "登录成功",
		"data": user,
	})
}
func List(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "3"))
	offend := (page - 1) * size
	/*
		SELECT
		  po_orders.id as OrderId,
		  pos.po_code as PoCode,
		  pos.po_name as PoName,
		  po_orders.order_code as OrderCode,
		  po_orders.order_good as OrderGood,
		  po_orders.order_to as OrderTo
		FROM
		  po_orders
		  LEFT JOIN pos ON pos.id = po_orders.pid
	*/
	var list []melsif.PoOrderList

	redis_key := "redis_list" + strconv.Itoa(page)
	po, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if po == "" {
		melsif.DB.Table("po_orders").
			Select("po_orders.id as OrderId",
				"pos.po_code as PoCode",
				"pos.po_name as PoName",
				"po_orders.order_code as OrderCode",
				"po_orders.order_good as OrderGood",
				"po_orders.order_to as OrderTo",
			).
			Joins("LEFT JOIN pos ON pos.id = po_orders.pid").
			Offset(offend).
			Limit(size).
			Find(&list)
		jsons, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsons, time.Hour*2)
	} else {
		json.Unmarshal([]byte(po), &list)
	}

	var count int64
	melsif.DB.Model(&melsif.PoOrder{}).Count(&count)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": gin.H{
			"count": count,
			"list":  list,
		},
	})
	return
}
func Xq(c *gin.Context) {
	id := c.Query("id")
	/*
		SELECT
		  po_orders.id as OrderId,
		  pos.po_code as PoCode,
		  pos.po_name as PoName,
		  po_orders.order_code as OrderCode,
		  po_orders.order_good as OrderGood,
		  po_orders.order_to as OrderTo
		FROM
		  po_orders
		  LEFT JOIN pos ON pos.id = po_orders.pid
		  WHERE
		  po_orders.id=1
	*/
	var list melsif.PoOrderList

	redis_key := "redis_xq" + id
	po, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if po == "" {
		melsif.DB.Table("po_orders").
			Select("po_orders.id as OrderId",
				"pos.po_code as PoCode",
				"pos.po_name as PoName",
				"po_orders.order_code as OrderCode",
				"po_orders.order_good as OrderGood",
				"po_orders.order_to as OrderTo",
			).
			Joins("LEFT JOIN pos ON pos.id = po_orders.pid").
			Where("po_orders.id=?", id).
			Find(&list)
		jsons, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsons, time.Hour*2)
	} else {
		json.Unmarshal([]byte(po), &list)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": list,
	})
	return
}
func Add(c *gin.Context) {
	OrderCode := c.PostForm("order_code")
	OrderGood := c.PostForm("order_good")
	OrderTo := c.PostForm("order_to")
	Pid, _ := strconv.Atoi(c.PostForm("pid"))

	if OrderCode == "" || OrderGood == "" || OrderTo == "" || Pid == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := melsif.PoOrder{
		OrderCode: OrderCode,
		OrderGood: OrderGood,
		OrderTo:   OrderTo,
		Pid:       Pid,
	}
	err := melsif.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}

	redis_key := "redis_add" + strconv.Itoa(int(data.ID))
	var add melsif.PoOrder
	melsif.DB.Where("id=?", data.ID).First(&add)
	jsons, _ := json.Marshal(add)
	cache.Rdb.Set(cache.Ctx, redis_key, jsons, time.Hour*2)

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func Del(c *gin.Context) {
	id := c.Query("id")
	melsif.DB.Where("id=?", id).Delete(&melsif.PoOrder{})

	redis_id := "redis_add" + id
	cache.Rdb.Del(cache.Ctx, redis_id)

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "删除成功",
	})
	return
}
